﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Acms.Bll
{
    public class DummyPost : Post
    {
        public static List<Post> CreateDummyList(int number)
        {
            Random rnd = new Random();
            var tmp = new List<Post>();
            while(tmp.Count < number)
            {
                tmp.Add(new DummyPost(rnd));
            }
            return tmp;
        }

        public DummyPost(Random rnd) : base() 
        {
            base.DisplayName = DisplayNames[rnd.Next(0, DisplayNames.Length)];
            base.Summary = SummaryTexts[rnd.Next(0, SummaryTexts.Length)];
            base.ThumbnailId = rnd.Next(0, 8) * -1; 
            Rnd = rnd; 
        }

        public Random Rnd { get; private set; }

        public new string Url
        {
            get { return "#";}
        }


        private string[] DisplayNames = new string[]
            {
                "Suspendisse vitae quam",
                "Quisque feugiat nibh vel lectus",
                "Duis faucibus auctor sapien",
                "Aliquam a felis non nisl",
                "Curabitur tristique sapien et risus"
            };

        private string[] SummaryTexts = new string[]
            {
                "In rutrum augue vel erat. Nulla sollicitudin nisi quis felis. Nulla eu neque cursus justo fermentum bibendum. Integer lorem. Mauris pharetra blandit est. Duis bibendum, sapien eu rhoncus auctor, metus odio scelerisque dui, sit amet pellentesque erat justo vel massa.",
                "Pellentesque bibendum, nisi non condimentum rutrum, leo est fermentum justo, at ultrices nisi enim non metus. Nulla sapien ipsum, congue sed, commodo quis, adipiscing eget, diam. Nullam consectetuer nibh et dui laoreet bibendum.",
                "Nunc rhoncus, dui non fringilla tincidunt, dolor nunc sodales libero, ac commodo pede nibh in quam. Vestibulum sit amet risus et risus cursus congue. Suspendisse potenti. Suspendisse sagittis urna eget justo. Integer nisi lorem, egestas in, posuere eu, tristique nec, lectus.",
                "Nullam tortor ante, faucibus et, commodo et, semper sit amet, turpis. Nam volutpat. Pellentesque porttitor tortor at nisi. Vivamus et magna ac est sagittis lacinia. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.",
                "Nunc rhoncus, dui non fringilla tincidunt, dolor nunc sodales libero, ac commodo pede nibh in quam. Vestibulum sit amet risus et risus cursus congue. Suspendisse potenti. Nulla vitae risus. Nam nec odio. Phasellus mollis, erat quis egestas sagittis, pede tortor hendrerit massa, nec sagittis odio magna fringilla eros. Mauris et tortor. Cras consequat sagittis mi. Sed venenatis adipiscing leo. Etiam dignissim tortor in leo."
            };

        protected override object SaveChanges(ref object concurencyId, Acms.SqlDal.Base dal)
        {
            return null;
        }

        protected override void Deleted()
        {
        }

        public int? NumberOfDummyChildren { get; set; }

        private List<Post> children;
        public override List<Post> Children
        {
            get
            {
                if (children == null)
                {
                    children = new List<Post>();
                    int count = Rnd.Next(10);
                    if(NumberOfDummyChildren.HasValue)
                    {
                        count = NumberOfDummyChildren.Value;
                    }
                    for(int i = 0; i < count; i++)
                    {
                        children.Add(new DummyPost(Rnd));
                    }
                }
                return children;
            }
        }

        private List<PostControl> controls = new List<PostControl>();
        public override List<PostControl> Controls
        {
            get
            {
                return controls;
            }
        }

        public override PostControl FindPostControl(string name)
        {
            foreach (PostControl control in Controls)
            {
                if (control.Name == name)
                {
                    return control;
                }
            }
            DummyPostControl tmp = new DummyPostControl(name, Rnd);
            controls.Add(tmp);
            return tmp;
        }
    }

    public class DummyPostControl : PostControl
    {
        public DummyPostControl(string name, Random rnd):base()
        {
            base.Name = name;
        }

    }

}
